package cn.codingjc.consumer.action;

import cn.codingjc.common.dto.DeptDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * @author shenjicheng
 * @date 2022/7/3 10:43 AM
 */

@RestController
@RequestMapping("/consumer/dept/*")
public class DeptConsumerAction {

    public static final String DEPT_ADD_URL = "http://127.0.0.1:8001/provider/dept/add";
    public static final String DEPT_GET_URL = "http://127.0.0.1:8001/provider/dept/get/";
    public static final String DEPT_LIST_URL = "http://127.0.0.1:8001/provider/dept/list";
    public static final String DEPT_SPLIT_URL = "http://127.0.0.1:8001/provider/dept/split";

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("add")
    public Object addDept(@RequestBody DeptDTO deptDTO){
        return this.restTemplate.postForObject(DEPT_ADD_URL, deptDTO, Boolean.class);
    }

    @GetMapping("get/{id}")
    public Object get(@PathVariable Long id){
        return this.restTemplate.getForObject(DEPT_GET_URL + id, DeptDTO.class);
    }

    @GetMapping("list")
    public Object list(){
        return this.restTemplate.getForObject(DEPT_LIST_URL, List.class);
    }

    @GetMapping("split")
    public Object list(@RequestParam int cp, @RequestParam int ls, @RequestParam String col, @RequestParam String kw){
        return this.restTemplate.getForObject(DEPT_SPLIT_URL + "?cp=" + cp + "&ls=" + ls + "&col="
                + col + "&kw=" + kw, Map.class);
    }
}
